//
//  ForgetPasswordViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/1.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "PublicForgetPwdApi.h"
#import "PublicGetCheckCodeApi.h"


@interface ForgetPasswordViewController ()


@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UITextField *verifyTF;
@property (nonatomic, strong) UITextField *passwordTF;
@property (nonatomic, strong) UIButton *submitButton;
@property (nonatomic, strong) UIButton *codeButton;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *passwordLabel;
@property (nonatomic, strong) UILabel *verifyLabel;
@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) UIView *line3;
@property (nonatomic, strong) UIButton *secretButton;


@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)refreshBaseControlValue
{
    
    [self setNavigationTitle:@"忘记密码" titleColor:[UIColor blackColor]];
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    
    [self masLayoutSubviews];
}


#pragma mark -- 事件
- (void)didClickSecretButton {
    
    self.secretButton.selected = !self.secretButton.selected;
    self.passwordTF.secureTextEntry = !self.secretButton.selected;
}

- (void) didClickSubmitButton {
    if (![_phoneTF.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (_verifyTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"亲，还未输入验证码..."];
        return;
    }
    
    if (_verifyTF.text.length < 6 || _verifyTF.text.length > 6) {
        [SVProgressHUD showErrorWithStatus:@"亲，请输入6位验证码..."];
        return;
    }
    
    if (![_passwordTF.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-18位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    [self confirmActionStart];
}

// 开始确定
- (void)confirmActionStart {
    PublicForgetPwdApi * forgetPwdApi = [[PublicForgetPwdApi alloc] initWithDic:@{@"username":self.phoneTF.text,@"password":self.passwordTF.text,@"vcode":self.verifyTF.text}];
    [forgetPwdApi dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary * dic = [forgetPwdApi responseJSONObject];
        
        if ([dic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.updateBlock) {
                    self.updateBlock(self.phoneTF.text);
                }
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }
    } failure:nil];
}


- (void) didClickCodeButton {
    if (![self.phoneTF.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    [self getVerifyCodeNumber];
    
}

// 获得验证码
- (void)getVerifyCodeNumber {
    [SVProgressHUD showInfoWithStatus:@"正在获取验证码..."];
    
    PublicGetCheckCodeApi * codeApi = [[PublicGetCheckCodeApi alloc] initWithDic:@{@"username":self.phoneTF.text,@"type":@"2"}];
    [codeApi dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary * codeDic = [request responseJSONObject];
        NSLog(@"code Dic == %@ ",codeDic);
        
        if ([codeDic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showInfoWithStatus:codeDic[@"errmsg"]];
            [self setVerifyBtnTimer];
            DTCSAVE_OBJECT(codeDic[@"vcode"], @"vcode");
        }else{
            [SVProgressHUD showErrorWithStatus:codeDic[@"errmsg"]];
        }

    } failure:nil];
    
}

// 设置定时器
- (void)setVerifyBtnTimer {
    __block int code_out = 60;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (code_out <= 0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeButton setTitle:@"获取验证码" forState:0];
                [self.codeButton setTitle:@"60秒后获取" forState:UIControlStateDisabled];
                self.codeButton.enabled = YES;
                code_out = 60;
            });
        } else {
            self.codeButton.enabled = NO;
            NSString * time = [NSString stringWithFormat:@"%d秒后获取",code_out];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.codeButton setTitle:time forState:UIControlStateDisabled];
            });
            code_out -- ;
        }
    });
    dispatch_resume(timer);
}


#pragma mark -- 懒加载
- (UITextField *)phoneTF
{
    if(!_phoneTF)
    {
        _phoneTF = [[UITextField alloc]init];
        _phoneTF.placeholder = @"Phone Number";
        _phoneTF.keyboardType = UIKeyboardTypePhonePad;
        [_phoneTF setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [self.view addSubview:_phoneTF];
    }
    return _phoneTF;
}

- (UITextField *)verifyTF
{
    if(!_verifyTF)
    {
        _verifyTF = [[UITextField alloc]init];
        _verifyTF.keyboardType = UIKeyboardTypePhonePad;
        [_verifyTF setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        _verifyTF.placeholder = @"Verification Code";
        [self.view addSubview:_verifyTF];
    }
    return _verifyTF;
}

- (UITextField *)passwordTF
{
    if(!_passwordTF)
    {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"New Password";
        _passwordTF.secureTextEntry = YES;
        [_passwordTF setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [self.view addSubview:_passwordTF];
    }
    return _passwordTF;
}

- (UIView *)line1
{
    if(!_line1)
    {
        _line1 = [[UIView alloc]init];
        _line1.backgroundColor = [UIColor colorWithHexString:@"#C9C9C9"];
        [self.view addSubview:_line1];
    }
    return _line1;
}
- (UIView *)line2
{
    if(!_line2)
    {
        _line2 = [[UIView alloc]init];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#C9C9C9"];
        [self.view addSubview:_line2];
        
    }
    return _line2;
}

- (UIView *)line3
{
    if(!_line3)
    {
        _line3 = [[UIView alloc]init];
        _line3.backgroundColor = [UIColor colorWithHexString:@"#C9C9C9"];
        [self.view addSubview:_line3];
        
    }
    return _line3;
}


- (UIButton *)codeButton
{
    if(!_codeButton)
    {
        _codeButton = [[UIButton alloc]init];
        [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_codeButton setTitleColor:[UIColor colorWithHexString:@"#231916"] forState:UIControlStateNormal];
        [_codeButton.titleLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_codeButton addTarget:self action:@selector(didClickCodeButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_codeButton];
    }
    return _codeButton;
}

- (UIButton *)secretButton
{
    if(!_secretButton)
    {
        _secretButton = [[UIButton alloc]init];
        [_secretButton setImage:[UIImage imageNamed:@"icon_hide"] forState:UIControlStateNormal];
        [_secretButton setImage:[UIImage imageNamed:@"icon_visual"] forState:UIControlStateSelected];
        [_secretButton addTarget:self action:@selector(didClickSecretButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_secretButton];
    }
    return _secretButton;
}

- (UIButton *)submitButton
{
    if(!_submitButton)
    {
        _submitButton = [[UIButton alloc]init];
        [_submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [_submitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _submitButton.layer.cornerRadius = 20;
        _submitButton.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [_submitButton addTarget:self action:@selector(didClickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_submitButton];
    }
    return _submitButton;
}


- (UILabel *)phoneLabel
{
    if(!_phoneLabel)
    {
        UIView *superView = self.view;
        _phoneLabel = [[UILabel alloc]init];
        [superView addSubview:_phoneLabel];
        [_phoneLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_phoneLabel setTextColor:[UIColor colorWithHexString:@"#3C3C3C"]];
        _phoneLabel.text = @"手机号";
    }
    return _phoneLabel;
}
- (UILabel *)passwordLabel
{
    if(!_passwordLabel)
    {
        UIView *superView = self.view;
        _passwordLabel = [[UILabel alloc]init];
        [superView addSubview:_passwordLabel];
        [_passwordLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_passwordLabel setTextColor:[UIColor colorWithHexString:@"#3C3C3C"]];
        _passwordLabel.text = @"密码";
    }
    return _passwordLabel;
}
- (UILabel *)verifyLabel
{
    if(!_verifyLabel)
    {
        UIView *superView = self.view;
        _verifyLabel = [[UILabel alloc]init];
        [superView addSubview:_verifyLabel];
        [_verifyLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_verifyLabel setTextColor:[UIColor colorWithHexString:@"#3C3C3C"]];
        _verifyLabel.text = @"验证码";
    }
    return _verifyLabel;
}

#pragma mark -- 初始化
- (void)masLayoutSubviews{
    __weak UIView *superView = self.view;
    CGFloat edge = px1080Width(60);
    CGFloat high = px1920Height(80);
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(px1920Height(242)+64);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line1).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.phoneTF.mas_top).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line1.mas_bottom).with.offset(px1920Height(245));
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(16));
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(px1920Height(60));
        make.width.mas_equalTo(px1080Width(300));
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#C9C9C9"];
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.right.equalTo(self.codeButton.mas_left).with.offset(-px1080Width(40));
        make.width.mas_equalTo(px1080Width(2));
    }];
    
    
    [self.verifyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(line.mas_left).with.offset(high);
    }];
    
    [self.verifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.verifyTF.mas_top).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    [self.line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2.mas_bottom).with.offset(px1920Height(245));
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    [self.secretButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line3).with.offset(-px1920Height(30));
        make.right.equalTo(self.view).with.offset(-2*edge);
        make.height.mas_equalTo(px1920Height(60));
        make.width.mas_equalTo(px1080Width(60));
    }];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line3).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(self.secretButton.mas_left).with.offset(-edge);
    }];
    
    
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.passwordTF.mas_top).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line3.mas_bottom).with.offset(px1920Height(224));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(500));
        make.height.mas_equalTo(px1920Height(136));
    }];
}
@end
